<template>
  <div>
    <div id="searchForm">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" ref="searchForm">
        <el-form-item label="" prop="username">
          <el-input v-model="searchForm.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item label="" prop="email">
          <el-input v-model="searchForm.email" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item label="" prop="active">
          <el-select v-model="searchForm.active" placeholder="请选择状态" style="width: 100%">
            <el-option label="激活" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchUser">查询</el-button>
          <el-button type="primary" @click="resetSearchFormData('searchForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" plain size="small" @click="add">新增</el-button>
    <el-button type="danger" plain size="small" @click="batchDel">批量删除</el-button>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄">
      </el-table-column>
      <el-table-column
        prop="email"
        width="180px"
        label="邮箱">
      </el-table-column>
      <el-table-column
        prop="active"
        width="180px"
        label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.active"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="激活"
            inactive-text="禁用"
            @change="changeState(scope.row.id,scope.row.active)"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="role_name"
        label="角色">
      </el-table-column>
      <el-table-column
        prop="dept_name"
        label="部门">
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间">
      </el-table-column>
      <el-table-column label="操作" width="280px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
          <el-button
            size="mini"
            @click="resetPwd(scope.row)"
            type="warning">重置密码
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
          <el-input v-model="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="active">
          <el-select v-model="form.active" placeholder="请选择状态" style="width: 100%">
            <el-option label="激活" :value="1"></el-option>
            <el-option label="禁用" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择状态" style="width: 100%">
            <el-option :label="role.role_name" :value="role.id" v-for="role in roles"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth" prop="dept_id">
          <el-select v-model="form.dept_id" placeholder="请选择部门" style="width: 100%">
            <el-option :label="dept.dept_name" :value="dept.id" v-for="dept in depts"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpDateUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>


import {addOrUpdate, changUserState, resetPwd, userBatchDelete, userOne, userPage} from "@/api/user";
import {deptList} from "@/api/dept";
import {roleList} from "@/api/role";

export default {
  name: 'index',
  data() {
    return {
      title: '新增',
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      pageSizes: [5, 10, 15, 20],
      pageSize: 5,
      total: 0,
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        age: [
          {required: true, message: '请输入年龄', trigger: 'blur'}
        ],
        active: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}
        ],
        role_id: [
          {required: true, message: '请选择角色', trigger: 'blur'}
        ],
        dept_id: [
          {required: true, message: '请选择部门', trigger: 'blur'}
        ]
      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        username: '',
        age: '',
        email: '',
        active: '',
        role_id: '',
        dept_id: ''
      },
      searchForm: {
        username: '',
        email: '',
        active: '',
      },
      roles: [],
      depts: []
    }
  },
  methods: {
    // 重置密码
    resetPwd(row) {
      resetPwd(row.id).then(response => {
        this.$notify.success(response.message)
      })
    },
// 重置搜索框
    resetSearchFormData(formName) {
      this.$refs[formName].resetFields();
      this.userPageData()
    }
    ,
// 搜索用户信息 分页
    searchUser() {
      // 将页码置为第一页
      this.currentPage = 1
      this.userPageData()
    }
    ,
    changeState(id, state) {
      changUserState({id, state}).then(response => {
        this.$notify.success(response.message)
        this.userPageData()
      })
    }
    ,
    // 编辑
    handleEdit(row) {
      this.roleSelect()
      this.deptSelect()
      this.title = '编辑'
      this.dialogFormVisible = true
      userOne(row.id).then(response => {
        this.form = response.data.user
      })
    }
    ,
// 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        let {id} = row
        // 发送异步请求
        userBatchDelete({ids: [id]}).then(response => {
          this.$notify.success(response.message)
          this.userPageData()
        })
      })
    }
    ,
// 复选 参数  val  表示当前选中行的数据
    handleSelectionChange(val) {
      this.multipleSelection = val.map(row => row.id)
    }
    ,
// 批量删除
    batchDel() {
      if (this.multipleSelection.length == 0) {
        this.$notify.warning('请至少选择一条数据')
        return
      }
      this.$confirm('删除选中数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        userBatchDelete({ids: this.multipleSelection}).then(response => {
          this.$notify.success(response.message)
          this.userPageData()
        })
      })
    }
    ,
// 新增
    add() {
      this.roleSelect()
      this.deptSelect()
      this.title = '新增'
      this.dialogFormVisible = true
    }
    ,
    addOrUpDateUser() {
      addOrUpdate(this.form).then(response => {
        this.$notify.success(response.message)
        this.userPageData()
        this.dialogFormVisible = false
      })
    }
    ,
    handleSizeChange(val) {
      this.pageSize = val
      // 重新获取分页数据
      this.userPageData()
    }
    ,
    handleCurrentChange(val) {
      this.currentPage = val
      this.userPageData()
    }
    ,
//关闭弹窗时 清除表单数据
    clearInfo() {
      this.form =
        {
          username: '',
          age: '',
          email: '',
          active: '',
          role_id: '',
          dept_id: ''
        }
      this.$refs.dialogForm.clearValidate()
    }
    ,
    userPageData() {
      userPage({
        page: this.currentPage,
        size: this.pageSize,
        username: this.searchForm.username,
        email: this.searchForm.email,
        active: this.searchForm.active
      }).then(response => {
        this.tableData = response.data.tableData
        this.total = response.data.total
      })
    }
    ,
    roleSelect() {
      roleList().then(response => {
        this.roles = response.data.roles
      })
    }
    ,
    deptSelect() {
      deptList().then(response => {
        this.depts = response.data.depts
      })
    }
  },
  created() {
    this.userPageData()
  }
}
</script>
<style scoped lang="scss">
#searchForm {
  border-bottom: 1px solid #ccc;
  margin-bottom: 15px;
}
</style>

